/*
 * ESPRSSIF MIT License
 *
 * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
 *
 * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP32 only, in which case,
 * it is free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"

#include "infra_compat.h"

#include "mqtt_solo.h"

#include "conn_mgr.h"

#include "common.h"

#include "http.h" //

/* The examples use simple WiFi configuration that you can set via
   'make menuconfig'.

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
// #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID // 
// #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD // 

#define EXAMPLE_WIFI_SSID "2-2-302" // 失效
#define EXAMPLE_WIFI_PASS "2-2-302-" // 失效

static const char *TAG = "app main";

static bool mqtt_started = false;

TaskHandle_t mqtt;
TaskHandle_t http;



static esp_err_t wifi_event_handle(void *ctx, system_event_t *event)
{
    switch (event->event_id)
    {
    case SYSTEM_EVENT_STA_GOT_IP:
        if (mqtt_started == false)
        {
            xTaskCreate((int (*)(void *))mqtt_main, "mqtt_example", 10240, NULL, 6, &mqtt);
            mqtt_started = true;
        }

        break;

    case SYSTEM_EVENT_STA_DISCONNECTED:

        break;
    default:
        break;
    }

    return ESP_OK;
}



// static esp_err_t wifi_event_handle(void *ctx, system_event_t *event)  
// {  
//     switch (event->event_id)  
//     {  
//         case SYSTEM_EVENT_STA_START:  
//             ESP_LOGI(TAG, "Wi-Fi station start");  
//             // esp_wifi_connect();  已经在conn_mgr_init();连接WIFI了
//             break;  
//         case SYSTEM_EVENT_STA_CONNECTED:  
//             ESP_LOGI(TAG, "Wi-Fi connected to ssid: %s", event->event_info.connected.ssid);  
//             // 这里可以添加代码来通知应用程序 Wi-Fi 已连接  
//             break;  
//         case SYSTEM_EVENT_STA_DISCONNECTED:  
//             ESP_LOGI(TAG, "Wi-Fi disconnected, reason: %d", event->event_info.disconnected.reason);  
//             esp_wifi_connect();  
//             // 这里可以添加代码来处理 Wi-Fi 断开连接的情况，比如重连等  
//             break;  
//         case SYSTEM_EVENT_STA_GOT_IP:  
//             ESP_LOGI(TAG, "Wi-Fi connected with IP address: " IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip));  
//             // 这里可以添加代码来启动 MQTT 连接等，如您的原始代码所示  
//             if (mqtt_started == false)  
//             {  
//                 xTaskCreate((void (*)(void *))mqtt_main, "mqtt_example", 10240, NULL, 5, NULL);  
//                 mqtt_started = true;  
//             }  
//             break;  
//         default:  
//             break;  
//     }  
//     return ESP_OK;  
// }


void app_main()
{
    configure_led();

    conn_mgr_init();
    conn_mgr_register_wifi_event(wifi_event_handle);

    // conn_mgr_set_wifi_config_ext((const uint8_t *)EXAMPLE_WIFI_SSID, strlen(EXAMPLE_WIFI_SSID), (const uint8_t *)EXAMPLE_WIFI_PASS, strlen(EXAMPLE_WIFI_PASS));
    
    IOT_SetLogLevel(IOT_LOG_INFO);

    conn_mgr_start();

    // xTaskCreate((void (*)(void *))key_test, "key_test", 10240, NULL, 5, NULL);
    
}
